{{define "Title"}}下载第三方包{{end}}
{{define "ContentWidth"}}8{{end}}
{{define "Breadcrumb"}}
	<ul class="breadcrumb">
		<li><a href="/"><i class="glyphicon glyphicon-home"></i> 首页</a></li>
		<li><a href="/packages">第三方包</a></li>
		<li class="active">下载</li>
	</ul>
{{end}}
{{define "Content"}}
	<div class="content">
		<div class="alert alert-success" role="alert">
		  <p>由于code.google.com被墙，导致一些托管在code.google.com上面的包go get不下来，此功能就是用于解决这个问题。
		  <p><strong>使用方法：</strong></p>
		  <p>输入完整包名，如：<code>code.google.com/p/go.net/websocket</code>，回车或点击【Go!】。如果包名输入正确的话，下面会有提示下载过程，如果服务器上go get正确完成，下载地址列表会列出所有相关的包，点击下载即可。</p>
		  <p>接下来介绍如何安装这些包：</p>
		  <p>先在你的GOPATH目录(如果不懂GOPATH，请先弄懂)下，创建目录src：</p>
		  <pre class="prettyprint">$ mkdir src</pre>
		  <p>复制code.google.com.p.go.net.tar.gz到src目录下，解压缩，这时候目录应该看起来是这个样子，</p>
		  <pre class="prettyprint">.
└── src
    └── code.google.com
        └── p
            └── go.net
                ├── ...
                └── websocket
                    ├── client.go
                    ├── exampledial_test.go
                    ├── examplehandler_test.go
                    ├── hybi.go
                    ├── hybi_test.go
                    ├── server.go
                    ├── websocket.go
                    └── websocket_test.go</pre>
		  <p>接下来安装这个包：</p>
		  <pre class="prettyprint">$ go install code.google.com/p/go.net/websocket</pre>
		  <p>这时候目录下会生成一个pkg目录，里面有编译好的.a文件，就可以在go文件里使用这个包了。</p>
		</div>
		<div class="input-group">
		  <input type="text" id="package" class="form-control" placeholder="输入完整包名" onkeypress="keypress();">
		  <span class="input-group-btn">
			<button class="btn btn-default" type="button" onclick="get_package();">Go!</button>
		  </span>
		</div>
		<div id="console-wrap" style="max-height: 200px; background-color: darkgreen; color: white; margin-top: 20px; border-radius: 4px; padding: 10px; overflow-y: auto;">
		  <pre id="console" style="background-color: transparent; border: none; border-radius: 0; padding: 0; color: white;"></pre>
		  <div id="waiting"></div>
		</div>
		<div class="panel panel-info">
		  <div class="panel-heading">下载地址</div>
		  <div class="panel-body" id="download">
			<ul>
            </ul>
		  </div>
		</div>
	</div>

    <script type="text/javascript">
	    var intervalID;

	    function keypress() {
		    if (event.keyCode == 13) {
		        get_package();
		    }
		}
	  
	    function get_package() {
		    $("#console").empty();
		    $("#download ul").empty();

		    var package = $("#package").val();
		    console.log(package);
		    if (package == "") {
		        alert("请输入包名称");
		        return;
            } else if (package.indexOf("/") == -1) {
                alert("请输入正确的第三方包名");
                return;
            }

			var sock = null;
			var wsurl = "ws://www.golangtc.com:8888/get/package";
				
			try {
				sock = new WebSocket(wsurl);
			} catch (e) {
				alert(e.Message);
			}

			sock.onopen = function () {
				console.log("connected to " + wsurl);
		        $('#console').append("$ go get -u -v " + package + "\n");
		        sock.send(package);
		        intervalID = setInterval(displayProcess, 1000);
			}

			sock.onerror = function (e) {
				console.log("err from connect " + e);
			}

			sock.onclose = function (e) {
				console.log("connection closed (" + e.code + ")");
			}

			sock.onmessage = function (e) {
				console.log("message received: " + e.data);
		        var data = eval('(' + e.data + ')');
		        if (data.type == "output") {
		            $("#console").append(data.msg + "\n");
                } else if (data.type == "command") {
		            $("#console").append("$ " + data.msg + "\n");
		        } else if (data.type == "download") {
                    $("#download ul").append('<li><a href="/static/download/packages/' + data.msg.replace(/\//g, '.') + '.tar.gz' + '" target="_blank">' + data.msg + '</li>');
                } else if (data.type == "completed") {
					$("#console").append("<strong>" + data.msg + "</strong>\n");
		            clearInterval(intervalID);
		            $('#waiting').empty();
				} else if (data.type == "error") {
					$("#console").append('<span style="color: red;">' + data.msg + '</span>\n');
		            clearInterval(intervalID);
		            $('#waiting').empty();
				}
																																	       $("#console-wrap").scrollTop($('#console')[0].scrollHeight);
			}
		}

		function input_package(name) {
		    $("#package").val(name);
		}

		function displayProcess() {
		    $('#waiting').append('.');
        }
	</script>
{{end}}

{{define "LeftSide"}}{{end}}

{{define "RightSide"}}
  <div class="col-md-4 sidebar">
	<div class="panel panel-default">
	  <div class="panel-heading">
		<h3 class="panel-title">Go 语言包管理</h3>
	  </div>
	  <div class="panel-body">
		<a href="http://gopm.io/" target="_blank">Go 语言包管理网站</a>提供类似的功能，无需 Git 和 Hg 等版本管理工具，就可以下载指定版本的 Go 语言包。也可以到该网站下载，网址：<a href="http://gopm.io" target="_blank">gopm.io</a>。
	  </div>
	</div>
	<div class="panel panel-default">
	  <div class="panel-heading">
		<h3 class="panel-title">最多下载</h3>
	  </div>
	  <ul class="list-group">
		{{range .packages}}
		<li class="list-group-item"><a href="javascript:;" onclick="input_package('{{.Name}}');">{{.Name}} <span class="badge pull-right">{{.Count}}</span></a></li>
		{{end}}
	  </ul>
	</div>
  </div>
{{end}}

